﻿using Ozner.Common;
using Ozner.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ozner.BLL
{
    public class UserBLL:BaseBLL
    {
        private UserBLL() { }
        private static UserBLL userBLL = new UserBLL();
        private User user = null;
        private string LoginUrl = "/handler/BaseUserLoginHandler.ashx";
        public BaseResult<JsonBaseResult<User>> Login(string account,string password, bool autologin)
        {
            string enPass = EncryptHelper.SHA1(password).ToLower();
            BaseResult<JsonBaseResult<User>> br = PostNoLoginedJson<JsonBaseResult<User>>(LoginUrl, string.Format("oper=login&userName={0}&passWord={1}", account, enPass));
            if(br.Code == Code.OK)
            {
                if(br.Object.Success == "true")
                {
                    //登录成功
                    ConfigModel.AutoLogin = autologin;
                    //设置cookie
                    ConfigModel.Cookies = "un=" + account + ";pd=" + password + ";" + br.ResponseCookies;
                    if (autologin)
                    {
                        ConfigModel.UserName = account;
                        ConfigModel.Password = password;   
                    }
                }
                else
                {
                    br.Message = br.Object.message;
                }
            }
            return br;
        }

        public static UserBLL GetInstance()
        {
            return userBLL;
        }
    }
}
